package com.rpc.core.codec.impl.kryo;



import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.rpc.core.holder.transmit.Response;


public class KryoResponseSerializer extends Serializer<Response>
{
    @Override
    public void write(Kryo kryo, Output output, Response object)
    {
        output.writeInt(object.getId());
        output.writeBoolean(object.isInvokeSuccess());
        if(object.isInvokeSuccess())
            kryo.writeClassAndObject(output, object.getResult());
        else
            kryo.writeClassAndObject(output, object.getThrowable());
    }

    @Override
    public Response read(Kryo kryo, Input input, Class<Response> type)
    {
        Response rpcResponse = new Response();
        rpcResponse.setId(input.readInt());
        rpcResponse.setInvokeSuccess(input.readBoolean());
        if(rpcResponse.isInvokeSuccess())
            rpcResponse.setResult(kryo.readClassAndObject(input));
        else
            rpcResponse.setThrowable((Throwable)kryo.readClassAndObject(input));

        return rpcResponse;
    }
}
